<?php

error_reporting(E_ERROR | E_WARNING | E_PARSE);

require_once("../functions.inc.php");
require_once("../database.inc.php");
require_once("./hoccleanup.inc.php");

$dbm = connect_db("dbm");
set_time_limit(5*3600);

$lastupdate = GetLastUpdate();

//cleanup

echo "fed_members cleanup\n";
$query = mysql_query(
	"SELECT t.userid, t.teamid, u.userid newuserid
	FROM flags_updated u, `teams_der` t
	where t.teamid = u.teamid
	and t.userid != 0
	and t.userid != u.userid", $dbm
) or die (mysql_error());

while ($row = mysql_fetch_array($query)) {
	mysql_query("DELETE FROM fed_members WHERE userid = {$row['userid']}", $dbm) or die (mysql_error());
	mysql_query("UPDATE teams_der SET userid = {$row['newuserid']} WHERE teamid = {$row['teamid']}", $dbm) or die (mysql_error());
	echo "deleting user {$row['userid']} from fed_members and altering teams_der because {$row['teamid']} has a new owner, {$row['newuserid']}\n";
}

echo "fed_members cleanup 2\n";
$query = mysql_query(
	"SELECT m.*
	FROM `fed_members` m
	LEFT JOIN `teams_$lastupdate` u ON m.userid = u.userid
	WHERE u.userid IS NULL
	AND membersince < '$lastupdate'", $dbm
) or die (mysql_error());

while ($row = mysql_fetch_array($query)) {
	mysql_query("DELETE FROM fed_members WHERE userid = {$row['userid']}", $dbm) or die (mysql_error());
	echo "deleted user {$row['userid']} from fed_members: not in teams_$lastupdate, but member of {$row['allianceid']} since {$row['membersince']}\n";
}

/*
echo "flags_v2 cleanup\n";
$query = mysql_query(
	"SELECT DISTINCT flags_v2.userid
	FROM flags_v2
	LEFT JOIN flags_updated ON flags_v2.userid = flags_updated.userid
	WHERE flags_updated.userid IS NULL", $dbm
) or die (mysql_error());

while ($row = mysql_fetch_array($query)) {
        mysql_query("DELETE FROM flags_v2 WHERE userid = {$row['userid']}", $dbm) or die (mysql_error());
        echo "deleted user {$row['userid']} from flags_v2 because they are no longer in flags_updated\n";
	ob_flush();
	flush();
}
*/

$query = mysql_query("SELECT SUM(activeusers) AS active FROM leagues_daily WHERE leagueid != 255", $dbm) or die (mysql_error());
$row = mysql_fetch_array($query);
$numactive = $row['active'];

echo "replace into flags_ranking\n";
if (!mysql_query("REPLACE INTO flags_ranking
	SELECT flags.userid userid, home, ROUND( SUM( $numactive / activeusers ) ) coolness, COUNT( DISTINCT flags.leagueid ) num
	FROM flags_v2 flags, leagues_daily
	WHERE flags.leagueid = leagues_daily.leagueid
/*	AND checked = 1 */
	GROUP BY userid, home", $dbm)) die ("flags 0+1 error: ". mysql_error());

echo "replace into flags_ranking 2\n";
if (!mysql_query("REPLACE INTO flags_ranking
	SELECT userid, 2 home, ROUND( 2 * SUM( $numactive / activeusers ) ) coolness, COUNT( DISTINCT flags_v2.leagueid ) num
	FROM flags_v2, leagues_daily
	WHERE flags_v2.leagueid = leagues_daily.leagueid
	GROUP BY userid", $dbm)) die ("flags 2 error: ". mysql_error());

echo "replace into flags_ranking 3\n";
if (!mysql_query("REPLACE INTO flags_ranking
	SELECT userid, 3 home, ROUND( SUM( $numactive / activeusers ) ) coolness, COUNT( flags.leagueid ) num
	FROM flags_v2 flags, leagues_daily
	WHERE flags.leagueid = leagues_daily.leagueid
/*	AND checked = 1 */
	GROUP BY userid", $dbm)) die ("flags 3 error: ". mysql_error());


clean_the_hoc($dbm, 100000, 10000);

?>
